const CryptoJs = require('crypto-js');
const baseURL = 'https://open1.chinaunicom.cn/api/jzjk/unifiedAcct';
const APPID = "W3OiWH0NCy";
const appsecret = "7OuIW7CICuuzujKEeUx3EkMiqBJyermZ";
const FROM_CHANNEL = "BJWeChatZ8";
var date = new Date();  
//年  
var Y = date.getFullYear();  
//月  
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);  
//日  
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();  
//时  
var h = (date.getHours()  <10 ? '0' + (date.getHours()):date.getHours());  
//分  
var m = (date.getMinutes() <10 ? '0' +(date.getMinutes()):date.getMinutes());  
//秒  
var s = (date.getSeconds() <10 ? '0' +(date.getSeconds()):date.getSeconds());  
//毫秒
var sss =date.getMilliseconds();
var TIMESTAMP = Y+'-'+M+'-'+D+' '+h+':'+m+':'+s+' '+sss; //时间戳

var RANDOM =  Math.floor(Math.random()*(999999-100000)+100000);//6位随机数

var TRANS_ID = Y+M+D+h+m+s+sss + RANDOM; //TRANS_ID

var TOKEN ='APP_ID'+APPID+'TIMESTAMP'+TIMESTAMP+'TRANS_ID'+TRANS_ID+appsecret;

function md5(str) {
   str = CryptoJs.MD5(str).toString();
 return str;
}
var TOKEN_MD5= md5(TOKEN);
 var state ="";

export function postRequest (api, postBody) {
  return new Promise((resolve,reject)=>{

  
    console.log({
        "UNI_BSS_HEAD": {
          "APP_ID": APPID,
          "TIMESTAMP": TIMESTAMP,
          "TRANS_ID": TRANS_ID,
          "TOKEN": TOKEN_MD5
        },
        "UNI_BSS_BODY": {
          "REQ": postBody
        }
      });
wx.request({
    url:baseURL+api,
    header: {
      "Content-Type": "application/json;charset=UTF-8",
      "Accept": "application/json",
      "Accept-Encoding": ""
    },
    method: "POST",
    data: {
      "UNI_BSS_HEAD": {
        "APP_ID": APPID,
        "TIMESTAMP": TIMESTAMP,
        "TRANS_ID": TRANS_ID,
        "TOKEN": TOKEN_MD5
      },
      "UNI_BSS_BODY": {
        "REQ": postBody
      }
    },
    success:function(res){
    if (res.data) {
      console.log(res.data)
     //  console.log(api + "called success");
       resolve(res.data)
      // return res.data;
    }
    else {
        console.log(res.data);
        return false;
        reject(res.data);
      }
    },
    fail(res){
      console.warn(res.data);
      return false;
   }
  })
})
}
export default {
    APPID,
    appsecret,
    TIMESTAMP,
    TRANS_ID,
    TOKEN,
    TOKEN_MD5,
    FROM_CHANNEL,
    postRequest
}
